package com.nuc.bigdata.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.exception.BusinessException;
import com.nuc.bigdata.model.ResultCode;
import com.nuc.bigdata.orm.dto.NewsCommentsDto;
import com.nuc.bigdata.orm.entity.CommentsLike;
import com.nuc.bigdata.orm.entity.NewsComments;
import com.nuc.bigdata.orm.feign.NewsCommentsFeign;
import com.nuc.bigdata.orm.mapper.CommentsLikeMapper;
import com.nuc.bigdata.orm.mapper.NewsCommentsMapper;
import com.nuc.bigdata.service.NewsCommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;

/**
 * @author JackLuo
 * @date 2019/11/20
 */
@Service
public class NewsCommentsServiceImpl implements NewsCommentsService {

    private final NewsCommentsMapper commentsMapper;
    private final CommentsLikeMapper commentsLikeMapper;
    private final static Long LENGTH = 3L;

    @Autowired
    NewsCommentsServiceImpl(NewsCommentsMapper commentsMapper,
                            CommentsLikeMapper commentsLikeMapper){
        this.commentsMapper = commentsMapper;
        this.commentsLikeMapper = commentsLikeMapper;
    }

    /**
     * 获取指定新闻的所有评论
     * @param newsId 新闻唯一标识
     * @param pageSize 页码
     * @return 包含给front服务的评论feign的ArrayList
     */
    @Override
    public ArrayList<NewsCommentsFeign> listById(String newsId,Long pageSize) {
        ArrayList<NewsCommentsFeign> commentsFeigns = new ArrayList<>();
        Long offset = (pageSize-1)*LENGTH;
        ArrayList<NewsComments> newsComments = commentsMapper.listByNewsId(newsId, offset, LENGTH);
        for(NewsComments comment:newsComments){
            NewsCommentsFeign commentsFeign = transformNewsComments(comment);
            commentsFeigns.add(commentsFeign);
        }
        return commentsFeigns;
    }


    /**
     * 创建评论
     * @param newsId 新闻唯一标识
     * @param commentsDto 评论内容类包含userId和content
     * @return 创建的评论
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createComments(String newsId, NewsCommentsDto commentsDto) {
        String commentId = IdUtil.fastSimpleUUID();
        NewsComments newsComments = new NewsComments();
        newsComments.setComment(commentsDto.getContent());
        newsComments.setNewsId(newsId);
        newsComments.setCommentId(commentId);
        newsComments.setCreatedBy(commentsDto.getUserId());
        commentsMapper.insert(newsComments);
    }

    /**
     * 点赞评论
     * @param userId 用户唯一标识
     * @param commentId 评论唯一标识
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toCommentLike(String userId, String commentId) {
        CommentsLike commentsLike = new CommentsLike();
        commentsLike.setCommentId(commentId);
        commentsLike.setCreatedBy(userId);
        QueryWrapper<CommentsLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id",commentId );
        queryWrapper.eq("created_by", userId);
        CommentsLike one = commentsLikeMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(one)){
            commentsLikeMapper.insert(commentsLike);
        }else {
            throw new BusinessException(ResultCode.COMMENT_LIKE_IS_EXIST,one);
        }
    }


    /**
     * 转化评论为评论feign类
     * @param newsComments 新闻评论实体
     * @return 新闻评论feign类
     */
    private NewsCommentsFeign transformNewsComments(NewsComments newsComments) {
        NewsCommentsFeign commentsFeign = new NewsCommentsFeign();
        commentsFeign.setCommentId(newsComments.getCommentId());
        commentsFeign.setNewsId(newsComments.getNewsId());
        commentsFeign.setComment(newsComments.getComment());
        commentsFeign.setCommentLike(newsComments.getCommentLike());
        commentsFeign.setCreatedBy(newsComments.getCreatedBy());
        commentsFeign.setCreateTime(newsComments.getCreateTime());
        return commentsFeign;
    }
}
